// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    content:cc.Node=null;

    _url=[];
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this._url = ["test assets/atlas", "test assets/prefab"];
    }

    private loadPrefab(){
        
        let url=this._url[1];

        this.releaseResource(url, cc.Prefab);

        
        //载入资源，并行错误处理
        cc.loader.loadRes(url,cc.Prefab,(err,prefab)=>{

            this.removeAllChildren();

            cc.loader.setAutoRelease(prefab,true);

            let node=cc.instantiate(prefab) as cc.Node;

            this.content.addChild(node);

            node.position=cc.v2(0,0);

        });
    }
    private loadSpriteFrame(){
        let url=this._url[0];

        this.releaseResource(url,cc.SpriteAtlas);

        cc.loader.loadRes(url,cc.SpriteAtlas,(err,atlas)=>{

            this.removeAllChildren();

            cc.loader.setAutoRelease(atlas,true);

            let node=new cc.Node();

            this.content.addChild(node);

            node.position=cc.v2(0,0);

            let sprite=node.addComponent(cc.Sprite);

            sprite.spriteFrame=atlas.getSpriteFrame('sheep_run_0');

        })
    }
    private removeAllChildren(){

        this.content.removeAllChildren(true);

    }

    private releaseResource(url,type){

        this.removeAllChildren();

        let res=cc.loader.getRes(url,type);

        let all=cc.loader.getDependsRecursively(res);

        cc.loader.release(all);
    }
    // update (dt) {}
}
